package com.zheng.study.base.bean.search.rule;

import com.zheng.study.base.bean.search.SearchEnum;
import org.springframework.util.Assert;

/**
 * 查询 - 参数 - RuleRelation
 * ============================================================================
 * author : fallenpanda
 * createDate:  2018/6/11 。
 * ============================================================================
 */
public class RuleRelation extends Rule {

	private final Rule[] rules;

	public RuleRelation(SearchEnum.RuleType ruleType, Rule[] rules) {
		super(ruleType);
		this.rules = rules;
	}

	public Rule[] getRules() {
		return rules;
	}

	public static RuleRelation and(Rule... rules){
		return newBuilder().setRuleType(SearchEnum.RuleType.and).setRules(rules).build();
	}

	public static RuleRelation or(Rule... rules){
		return newBuilder().setRuleType(SearchEnum.RuleType.or).setRules(rules).build();
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public static class Builder extends Rule.Builder<Rule, Builder>{

		private Rule[] rules;

		public Builder setRules(Rule... rules) {
			this.rules = rules;
			return this;
		}

		@Override
		protected Builder getThis() {
			return this;
		}

		@Override
		public RuleRelation build() {
			Assert.notNull(ruleType, "ruleType 不能为空");
			Assert.notEmpty(rules, "ruleType 不能为空");
			return new RuleRelation(ruleType, rules);
		}
	}

}
